#!/usr/bin/env python

#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

"""
Functions and such for approval of submissions on the editor page.
"""

from directory import *
import re

# if another editor already handled this submission
def alreadyDone():
	return '<b>Submission missing.</b> Another editor has likely already handled it.'


# approval summary
def approvalSummary(submis):
	print '<table class="smallish" width="100%">'
	# identify type
	type = getDirType(submis)

	# output submission
	print """
	<tr><td class="expand_bg">Title:</td><td>%s</td></tr>
	<tr><td class="expand_bg">Description:</td><td>%s</td></tr>
	""" % ( uni2asc(submis['entryTitle']),nl2br(uni2asc(submis['entryDescription'])) )
	if type == 'Record' : print '<tr><td class="expand_bg">URL:</td><td><a href="%s">%s</a></td></tr>' % (submis['entryLink'], submis['entryLink'])
	print """
	<tr><td class="expand_bg">Type:</td><td>%s</td></tr>
	<tr><td class="expand_bg">Email:</td><td>%s</td></tr>
	""" % (type, submis['optionalEmail'])

	# external or metadata
	metadatalink = 'No metadata; external link.'
	if submis['isExternal'] == 'N':
		metadatalink = '<a href="search.py?s=rec%%3a%s">View metadata</a>' % submis['recHash']

	print """
	<tr><td class="expand_bg">Metadata: </td><td>%s</td></tr>
	""" % metadatalink

	# breadcrumbs
	dirOut = directoryOutput([],0)
	if type == 'Link':
		print '<tr><td class="expand_bg">Link Location: </td><td>'
		dirOut.printCrumbs(submis['parentHash'],1)
		print '</td></tr>'
		print '<tr><td class="expand_bg">Links To: </td><td>'
		dirOut.printCrumbs(submis['targetHash'],1)
		print '</td></tr>'

	else:
		print '<tr><td class="expand_bg">Directory: </td><td>'
		dirOut.printCrumbs(submis['parentHash'],1)
		print '</td></tr>'

	print '</table>'
	

# split compare
def splitCompare(submis):
	print '<div>'
	print '<b>MODIFICATION</b>'
	approvalSummary(submis)
	print '</div>'
	print '<div>'
	print '<b>CURRENT</b>'
	try:
		current = quick_query("SELECT * FROM directoryTree WHERE recHash = '%s';" % submis['recHash'])
		current = current[0]
		current['optionalEmail'] = 'N/A'
		approvalSummary(current)
	except: pass
	print '</div>'

# print modification submission
def outModify(divNum, submis):
	# detect deletion
	detectdel = re.compile('\s*PLEASE DELETE THIS')
	deletion = 0
	if detectdel.match(submis['entryDescription']) is not None:
		deletion = 1
	# overview
	if deletion: approvalSummary(submis)
	else: splitCompare(submis)

	if deletion:
		if submis['isDirectory'] == 'Y' :
			print """This is a request for deleting the specified entry.
			Any entry below the current entry will be relocated to the
			same level in the directory that the deleted entry was."""
		else:
			print """This is a request for deleting the specified entry.
			Once an entry is deleted, it cannot be recovered."""
			
	else: print 'This is a modification request. Please review any changes before approving/rejecting the submission.'
	print '<br><br>'
	# approve
	print '<div id="subact%s" align="center">' % divNum
	if not deletion: print '<span class="midsized"><a class="fakelink" onClick="sendAR(%s,\'a\');">Approve Modify</a> &nbsp;&nbsp;' % divNum
	# delete
	if deletion : print '<a class="fakelink" onClick="confirmDelete(%s);">Approve Deletion</a> &nbsp;&nbsp;' % divNum
	# edit
	print '<a class="fakelink" onClick="editSuggestion(%s);">Edit Suggestion</a> &nbsp;&nbsp;' % divNum
	# reject
	print '<a class="fakelink" onClick="rejectOption(%s);">Reject Suggestion</a></span>' % divNum
	print '</div>'


# print submission
def outSubmission(divNum, submis):
	# overview
	approvalSummary(submis)

	print 'This is a new submission.<br><br>'
	# approve
	print '<div id="subact%s" align="center">' % divNum
	print '<span class="midsized"><a class="fakelink" onClick="sendAR(%s,\'a\');">Approve Submission</a> &nbsp;&nbsp;' % divNum
	# edit
	print '<a class="fakelink" onClick="editSuggestion(%s);">Edit Suggestion</a> &nbsp;&nbsp;' % divNum
	# reject
	print '<a class="fakelink" onClick="rejectOption(%s);">Reject Suggestion</a></span>' % divNum
	print '</div>'

# eof
